import { useResponseSuccess } from '~/utils/response';

export default eventHandler(async (event) => {
  const query = getQuery(event);
  const { keyword, alertType, severity, page = 1, pageSize = 10 } = query;

  // 模拟预警数据
  const alerts = [
    {
      id: '1',
      resourceName: '温泉池',
      alertType: 'LOW_STOCK',
      severity: 'HIGH',
      message: '库存数量低于最小阈值',
      currentValue: '1',
      threshold: '2',
      status: 'ACTIVE',
      createTime: '2024-01-15 10:30:00',
    },
    {
      id: '2',
      resourceName: 'VIP包间',
      alertType: 'LOW_STOCK',
      severity: 'CRITICAL',
      message: '库存数量为0，需要紧急补充',
      currentValue: '0',
      threshold: '1',
      status: 'ACTIVE',
      createTime: '2024-01-15 09:15:00',
    },
    {
      id: '3',
      resourceName: '豪华套房',
      alertType: 'OVERSTOCK',
      severity: 'MEDIUM',
      message: '库存积压，建议促销',
      currentValue: '8',
      threshold: '5',
      status: 'ACTIVE',
      createTime: '2024-01-15 08:45:00',
    },
    {
      id: '4',
      resourceName: '数字会议室',
      alertType: 'MAINTENANCE',
      severity: 'LOW',
      message: '设备需要定期维护',
      currentValue: '正常',
      threshold: '30天',
      status: 'RESOLVED',
      createTime: '2024-01-14 16:20:00',
    },
  ];

  // 过滤数据
  let filteredData = alerts;

  if (keyword) {
    filteredData = filteredData.filter(
      (item) =>
        item.resourceName.includes(keyword as string) ||
        item.alertType.includes(keyword as string),
    );
  }

  if (alertType) {
    filteredData = filteredData.filter((item) => item.alertType === alertType);
  }

  if (severity) {
    filteredData = filteredData.filter((item) => item.severity === severity);
  }

  // 分页
  const start = (Number(page) - 1) * Number(pageSize);
  const end = start + Number(pageSize);
  const items = filteredData.slice(start, end);

  return useResponseSuccess({
    items,
    total: filteredData.length,
    page: Number(page),
    pageSize: Number(pageSize),
  });
});
